// 再看更符合 Go 设计惯例的实现，也就是使用无缓冲 channel 替代锁后的实现：
// 将计数器操作全部交给一个独立的 Goroutine 去处理，并通过无缓冲 channel 的同步阻塞特性，实现了计数器的控制。
// 这样其他 Goroutine 通过 Increase 函数试图增加计数器值的动作，实质上就转化为了一次无缓冲 channel 的接收动作。
// 这种并发设计逻辑更符合 Go 语言所倡导的“不要通过共享内存来通信，而是通过通信来共享内存”的原则。
package main

import (
	"fmt"
	"sync"
)

type counter struct {
	c chan int
	i int
}

func NewCounter() *counter {
	cter := &counter{
		c: make(chan int),
	}
	go func() {
		for {
			cter.i++
			cter.c <- cter.i
		}
	}()
	return cter
}
func (cter *counter) Increase() int {
	return <-cter.c
}
func main() {
	cter := NewCounter()
	var wg sync.WaitGroup
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func(i int) {
			v := cter.Increase()
			fmt.Printf("goroutine-%d: current counter value is %d\n", i, v)
			wg.Done()
		}(i)
	}
	wg.Wait()
}

/*
goroutine-0: current counter value is 1
goroutine-3: current counter value is 7
goroutine-9: current counter value is 3
goroutine-7: current counter value is 6
goroutine-2: current counter value is 10
goroutine-6: current counter value is 5
goroutine-4: current counter value is 8
goroutine-1: current counter value is 2
goroutine-8: current counter value is 9
goroutine-5: current counter value is 4
*/
